import java.util.Scanner;


public class Password{
    public static void main(String[] args){
       
        Password head = new Password();
        Password password = new Password();
        
        head.word();
        

        Scanner in = new Scanner(System.in);
        Scanner sc = new Scanner(System.in);
        int choice =sc.nextInt();
        for(;0<choice&&choice<=5;System.out.println("请继续选择操作"),choice =sc.nextInt()){
        if(choice == 1){
        
         

public class Password{   
    public static void main(String[] args){     //各个功能已经在源代码文件注释 便不在Git上另外注释
        Scanner in = new Scanner(System.in);      //Hxl修改
        Password password = new Password();      

        System.out.println("请输入要加密的密码");
        String input = in.nextLine();
        String encryptedPassword = password.encrypt(input);   //调用加密方法
        System.out.println("加密后的密码是"+encryptedPassword);

        
        }
        else if(choice==2){
        
        

        System.out.println("请输入要解密的密码");
        String input2 = in.nextLine();
        String decryptedPassword = password.decrypt(input2);
        System.out.println("解密后的密码是"+decryptedPassword);


        }
        else if(choice==3){
             System.out.println("输入您要判断的密码");
            String password4 = in.nextLine();
            System.out.println(checkPasswordStrength(password4));

           



        }
        else if(choice==4){
            System.out.println("请输入要生成的密码位数");
            int length = sc.nextInt();
            System.out.println("新生成的密码是"+generateRandomPassword(length));
            
        }
         else if(choice==5){
            System.out.println("您已成功退出程序");
            System.exit(0);

        }
    }
        
           
    }

    public  String encrypt(String password){   //加密方法

    }

    public static String encrypt(String password){   //加密方法


        if(password.length()>16){   //当密码长度超过16，抛出异常
            throw new IllegalArgumentException("密码长度不能超过16个字符");
        }



        StringBuilder encrypted = new StringBuilder();

        for (int i = 0; i < password.length(); i++) {
            char c = password.charAt(i);
            char encryptedChar = (char)(c + i + 1 + 3);
            encrypted.append(encryptedChar);
        }

        char firstChar = encrypted.charAt(0);
        encrypted.setCharAt(0, encrypted.charAt(encrypted.length()-1));
        encrypted.setCharAt(encrypted.length()-1, firstChar);

        return encrypted.reverse().toString();

    }

    public  String decrypt(String input){    //解密方法
        StringBuilder decrypted = new StringBuilder(input).reverse();

        char firstChar = decrypted.charAt(0);
        decrypted.setCharAt(0, decrypted.charAt(decrypted.length() - 1));
        decrypted.setCharAt(decrypted.length() - 1, firstChar);

        for (int i = 0; i < decrypted.length(); i++) {
            int dcPw = (int)decrypted.charAt(i) - i - 1 - 3;
            decrypted.setCharAt(i, (char)dcPw);
        }

        return decrypted.toString();


    }

    public static String generateRandomPassword(int length) {  // 生成指定长度的随机密码
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()";
        StringBuilder password = new StringBuilder();
    
        
        for (int i = 0; i < length; i++) {
            int randomIndex = (int)(Math.random() * characters.length());
            password.append(characters.charAt(randomIndex));
        }
    
        return password.toString();
    }

    public static String checkPasswordStrength(String password) {
        if (password.length() < 8) {
            System.out.println("密码长度太短，建议8个字符以上");;
        }
    
        boolean containsUppercase = false;
        boolean containsLowercase = false;
        boolean containsDigit = false;
    
        for (char c : password.toCharArray()) {       //判断密码强度的方法
            if (Character.isUpperCase(c)) {
                containsUppercase = true;
            } else if (Character.isLowerCase(c)) {
                containsLowercase = true;
            } else if (Character.isDigit(c)) {
                containsDigit = true;
            }
        }
    
        if (!containsUppercase || !containsLowercase || !containsDigit) {
            System.out.println("建议密码包含大写字母、小写字母和数字");
        }
    
        System.out.println("密码强度符合要求");
        return password;
    }
    
    public  String word(){                                   //呈现菜单的方法
        int length = 30;                                     // 定义双横线的长度
        for (int i = 0; i < length; i++) {
            System.out.print("=");
        }
        System.out.println("");
        System.out.println("     欢迎使用密码管理系统");
        System.out.println("     请选择操作：");
        System.out.println("   1.加密");
        System.out.println("   2.解密");
        System.out.println("   3.判断密码强度");
        System.out.println("   4.密码生成");
        System.out.println("   5.退出");
        System.out.println("   请输入您的选择（输入序号）");                            //换行符
                                         
        for (int i = 0; i < length; i++) {
            System.out.print("=");
        }
        System.out.println("");                            //换行符
        return null;
    }



        StringBuilder encrypted = new StringBuilder();

        for(int i=0;i<password.length();i++){

            char c = password.charAt(i);   
            char encryptedChar = (char)(c+i+1+3);   ////将每个字符的ASCII码加上它在字符串中的位置（从位置1开始）和偏移值3
            encrypted.append(encryptedChar);   
  
        }

        char firstChar = encrypted.charAt(0);
        encrypted.setCharAt(0, encrypted.charAt(encrypted.length()-1));   //将字符串的首位和尾位进行调换
        encrypted.setCharAt(encrypted.length()-1, firstChar);

        char[] chars = encrypted.toString().toCharArray();   //将encrypted对象转换为字符串再转换为字符数组存储在char变量中

        for(int i = encrypted.length()/2;i>=0;i--){   //反转

            char temp = chars[i];
            chars[i] =chars[chars.length-1-i];
            chars[chars.length-1-i] = temp; 

        }

        return new String(chars);

    }

    public  static String decrypt(String input){
        StringBuilder decrypted = new StringBuilder();

        char[] dc = new char[input.length()];
        for(int i = dc.length-1;i>=0;i--){

            dc[input.length()-1-i]=input.charAt(i);


        }

        char firstChar = dc[dc.length-1];
        dc[dc.length-1]=dc[0];
        dc[0]=firstChar;

        for(int i = 0;i<dc.length;i++){
            int dcPw = (int)dc[i]-i-1-3;
            dc[i] = (char)dcPw;
        }

        return new String(dc);

      

    }

}
